<?php

class Pilotage_AcceuilController extends Zend_Controller_Action
{

    public function init()
    {
    	
    	
    	if (Zend_Auth::getInstance ()->hasIdentity ()) {
    		//$this->_helper->redirector ( 'menu','acceuil' );
    	} else {
    		//$this->_helper->redirector ( 'index','acceuil' );
        	//$this->_helper->redirector ( 'index','acceuil','pilotage' );
    	}
    	
    	
        /* Initialize action controller here */
    }

    protected function _process($values)
    {
    	// Get our authentication adapter and check credentials
    	$adapter = $this->_getAuthAdapter();
    	$adapter->setIdentity($values['login']);
    	$adapter->setCredential($values['motpass']);
    
    	$auth = Zend_Auth::getInstance();
    	$result = $auth->authenticate($adapter);
    	if ($result->isValid()) {
    		$user = $adapter->getResultRowObject();
    		$auth->getStorage()->write($user);
    		return true;
    	}
    	return false;
    }
    
    protected function _getAuthAdapter() {
    
    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
    	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    
    	$authAdapter->setTableName('personnes')
    	->setIdentityColumn('login')
    	->setCredentialColumn('motpass');
    	//->setCredentialTreatment('SHA1(CONCAT(?,salt))');
    
    	return $authAdapter;
    }
    
    public function indexAction()
    {
        // action body
    	$form = new Application_Form_authentification();
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			if ($this->_process($form->getValues())) {
    				// We're authenticated! Redirect to the home page
    				    				$this->_helper->redirector('menu','acceuil');
    			}
    		}
    	}
    	$this->view->form = $form;
    }

    public function menuAction()
    {
        // action body
        
    	
    	
    	$this->_helper->layout()->disableLayout();
    	//$this->_helper->layout()->setLayout("gestionlayout");
    	
    	$AFA = new Application_Form_Acceuil();
    	$AFA->setAction('menu');
    	 
    	$req=$this->getRequest();
    	//print_r($req->getPost());
    	 
    	 
    	$this->view->Formulaireacceuil= $AFA;
    	
    }

    public function testAction()
    {
        // action body
    	$AFA = new Application_Form_Acceuil();
    	$AFA->setAction('test');
    	
    	$req=$this->getRequest();
    	//print_r($req->getPost());
    	
    	
    	$this->view->Formulaireacceuil= $AFA;
    	
    }

    
	public function loginAction()
       {
       	
       	
        $form = new Application_Form_Authentification();
        $this->view->form=$form;
        if ($this->_request->isPost ()) {
         $formData = $this->_request->getPost ();
         if ($form->isValid ( $formData )) {
          $Login = $this->_request->getParam('Login');
                $Password = $this->_request->getParam('Password');
          $authAdapter = new Zend_Auth_Adapter_DbTable ( Zend_Db_Table::getDefaultAdapter () );
          $authAdapter->setTableName ( 'personnes' )
          ->setIdentityColumn ( 'login' )
          ->setCredentialColumn ( 'motpass' )
          ->setIdentity ( $Login )
          ->setCredential ( $Password );
          $authAuthenticate = $authAdapter->authenticate ();
          if ($authAuthenticate->isValid ()) {
           $storage = Zend_Auth::getInstance ()->getStorage ();
           $storage->write ( $authAdapter->getResultRowObject ( null, 'PASSE' ) );
           $this->_redirect('login');
            
           
         } else {
          $this->view->loginMessage = "Nom utilisateur ou mot de passe invalide!";
          
          
         }
        }
        $this->view->form = $form;
        }
       }
    
    public function logoutAction() 
    {
    	Zend_Auth::getInstance ()->clearIdentity ();
    	$this->_helper->redirector ( 'index', 'acceuil' );
    }
    
    /*
    public function preDispatch() {
    	if (Zend_Auth::getInstance ()->hasIdentity ()) {
    		if ('logout' != $this->getRequest ()->getActionName ()) {
    			//$this->_helper->redirector('index','acceuil');
    			//$this->_helper->redirector ( 'menu','acceuil' );
    		}
    	} else {
    		//$this->_helper->redirector ( 'index','acceuil' );
    		echo $this->getRequest ()->getActionName ();
    		
    		if ('logout' == $this->getRequest ()->getActionName ()) {
    			$this->_helper->redirector ( 'index','acceuil' );
    		}
    	}
    }
    */
    
    
}